import { Provide } from '@midwayjs/decorator';
import { BaseService } from '@cool-midway/core';
import { InjectEntityModel } from '@midwayjs/orm';
import { Repository } from 'typeorm';
import { OriginalInfoEntity } from '../entity/info';
import * as MyBigNumber from 'my-bignumber';
import {  CoolCommException } from '@cool-midway/core';

/**
 * 描述
 */
@Provide()
export class OriginalInfoService extends BaseService {
  @InjectEntityModel(OriginalInfoEntity)
  originalInfoEntity: Repository<OriginalInfoEntity>;

  /**
   * 查询插画总价
   */
  async price(query: string) {
    const list = await this.lists(query);
    let totalPrice = 0;
    if (list && list.length) {
      totalPrice = list.reduce((sum, item) => {
        return MyBigNumber.plus(sum, item.price);
      }, 0);
    }
    return totalPrice;
  }
  /**
   * 查询插画
   */
  async lists(query: string) {
    let ids = query.split(',');
    if (ids) {
      return await this.originalInfoEntity
        .createQueryBuilder()
        .select()
        .whereInIds(ids)
        .getMany();
    } else {
        throw new CoolCommException('参数错误！');
    }
  }
}
